package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.PageResult2;
import com.itheima.common.PermissionsGg;
import com.itheima.entity.Permission;
import com.itheima.entity.PermissionGroup;
import com.itheima.entity.PermissionGroupDto;
import com.itheima.entity.PermissionPermissionGroup;
import com.itheima.mapper.PermissionGroupMapper;
import com.itheima.mapper.PermissionMapper;
import com.itheima.mapper.PermissionPermissionGroupMapper;
import com.itheima.service.PermissionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author cfr
 * @date 2022/1/11
 * @TIME:20:51
 */
@Service
public class PermissionsServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionsService {
    @Autowired
    private PermissionPermissionGroupMapper permissionPermissionGroupMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private PermissionGroupMapper permissionGroupMapper;

    @Override
    public String deleteById(Integer id) {
        LambdaQueryWrapper<PermissionPermissionGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PermissionPermissionGroup::getPgid, id);
        permissionPermissionGroupMapper.delete(wrapper);
        permissionGroupMapper.deleteById(id);
        return "scuess";
    }

    @Override
    public PageResult2 findPage(Integer page, Integer pagesize, String title) {
        IPage<PermissionGroup> p = new Page<>(page, pagesize);
        LambdaQueryWrapper<PermissionGroup> wrapper = new LambdaQueryWrapper<>();
        if (null != title) {
            wrapper.like(PermissionGroup::getName, title);
        }
        IPage<PermissionGroup> permissionGroupIPage = permissionGroupMapper.selectPage(p, wrapper);
        String s = JSON.toJSONString(permissionGroupIPage.getRecords());
        List<PermissionGroupDto> permissionGroupDtos = JSON.parseArray(s, PermissionGroupDto.class);
        for (PermissionGroupDto permissionGroupDto : permissionGroupDtos) {
            permissionGroupDto.setTitle(permissionGroupDto.getName());
            permissionGroupDto.setCreate_date(permissionGroupDto.getCreateTime());
            permissionGroupDto.setUpdate_date(permissionGroupDto.getUpdate_date());
        }
        PageResult2 pageResult2 = new PageResult2();
        pageResult2.setCounts((int) permissionGroupIPage.getTotal());
        pageResult2.setPage(page);
        pageResult2.setPages((int) p.getPages());
        pageResult2.setPagesize(pagesize);
        pageResult2.setList(permissionGroupDtos);
        return pageResult2;
    }

    @Override
    public String add(PermissionsGg permissions) {
        PermissionGroup permissionGroup = new PermissionGroup();
        permissionGroup.setName(permissions.getTitle());
        permissionGroup.setCreateTime(new Date());
        permissionGroup.setUpdateTime(new Date());
        permissionGroupMapper.insert(permissionGroup);
        for (Integer permission : permissions.getPermissions()) {
            if (permission == 0) {

            } else {
                PermissionPermissionGroup permissionPermissionGroup = new PermissionPermissionGroup();
                permissionPermissionGroup.setPgid(permissionGroup.getId());
                permissionPermissionGroup.setPid(Long.valueOf(permission));
                permissionPermissionGroupMapper.insert(permissionPermissionGroup);
            }
        }
        return "scuess";
    }

    @Override
    public PermissionsGg findById(Integer id) {
        PermissionGroup permissionGroup = permissionGroupMapper.selectById(id);

        PermissionsGg permissionsGg = new PermissionsGg();
        permissionsGg.setCreate_date(permissionGroup.getCreateTime());
        permissionsGg.setTitle(permissionGroup.getName());
        permissionsGg.setId(id);
        Integer[] ccc = permissionPermissionGroupMapper.selectId(id);
        permissionsGg.setPermissions(ccc);


        return permissionsGg;
    }

    @Override
    public String modify(PermissionsGg permissionsGg) {
        //删之前的
        LambdaQueryWrapper<PermissionPermissionGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PermissionPermissionGroup::getPgid, permissionsGg.getId());
        permissionPermissionGroupMapper.delete(wrapper);
        //添加新的
        Integer[] permissions = permissionsGg.getPermissions();
        for (Integer permission : permissions) {
            if (permission == 0) {

            } else {
                PermissionPermissionGroup permissionPermissionGroup = new PermissionPermissionGroup();
                permissionPermissionGroup.setPgid(Long.valueOf(permissionsGg.getId()));
                permissionPermissionGroup.setPid(Long.valueOf(permission));
                permissionPermissionGroupMapper.insert(permissionPermissionGroup);
            }
        }
        //更新时间,名字
        PermissionGroup permissionGroup = permissionGroupMapper.selectById(permissionsGg.getId());
        permissionGroup.setName(permissionGroup.getName());
        permissionGroup.setUpdateTime(new Date());
        permissionGroupMapper.updateById(permissionGroup );
        return "scuess";
    }
}
